﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace ZuoAlgorithms.Class009;

// 按值传递、按引用传递
// 从堆栈角度解释链表节点
// 以堆栈视角来看链表反转
public class ListReverse
{
    public void F(int a)
    {
        a = 0;
    }

    public class Number
    {
        public int val;
        public Number(int v)
        {
            val = v;
        }
    }

    public static void G1(Number b)
    {
        b = null;
    }

    public static void G2(Number b)
    {
        b.val = 6;
    }

    public static void G3(int[] c)
    {
        c = null;
    }

    public static void G4(int[] c)
    {
        c[0] = 100;
    }

    // 单链表节点
    public class ListNode
    {
        public int val;
        public ListNode next;
        public ListNode(int val)
        {
            this.val = val;
        }

        public ListNode(int val, ListNode next)
        {
            this.val = val;
            this.next = next;
        }
    }

    // 反转单链表测试链接 : https://leetcode.cn/problems/reverse-linked-list/
    class Solution
    {
        public ListNode ReverseList(ListNode head)
        {
            ListNode pre = null;
            ListNode next = null;
            while (head != null)
            {
                next = head.next;
                head.next = pre;
                pre = head;
                head = next;
            }
            return pre;
        }
    }


    // 双链表节点
    public class DoubleListNode
    {
        public int value;
        public DoubleListNode last;
        public DoubleListNode next;
        public DoubleListNode(int v)
        {
            value = v;
        }
    }

    // 反转双链表
    public DoubleListNode ReverseDoubleList(DoubleListNode head)
    {
        DoubleListNode pre = null;
        DoubleListNode next = null;
        while (head != null)
        {
            next = head.next;
            head.next = pre;
            head.last = next;
            pre = head;
            head = next;
        }
        return pre;

    }

}


